package com.fhz.day007;

import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.fhz.day007.core.BaseActivity;
import com.fhz.day007.databinding.ActivityMainBinding;
import com.fhz.day007.entity.Category;
import com.fhz.utilsfhz.ApiResponse;

import java.util.List;

public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> {

    private static final String TAG = "feng";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        //初始化数据
//        mViewModel.getCategroy(1).observe(this, new Observer<ApiResponse<List<Category>>>() {
//            @Override
//            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
//                Log.i(TAG, "onChanged: "+listApiResponse.data.get(1).getCategoryName());
//            }
//        });
//
//        mViewModel.getCategroy(2).observe(this, new Observer<ApiResponse<List<Category>>>() {
//            @Override
//            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
//                Log.i(TAG, "onChanged: "+listApiResponse.data.get(1).getCategoryName());
//            }
//        });

        mBinding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewModel.getCategroy(2).observe(MainActivity.this, new Observer<ApiResponse<List<Category>>>() {
                    @Override
                    public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                        mViewModel.getMutableLiveData().getValue().getData().addAll(listApiResponse.getData());
                        mViewModel.getMutableLiveData().setValue(mViewModel.getMutableLiveData().getValue());
                    }
                });
            }
        });

        //10个
        mViewModel.getCategroy(1).observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                mViewModel.getMutableLiveData().setValue(listApiResponse);
            }
        });

        mViewModel.getMutableLiveData().observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                Log.i(TAG, "onChanged: "+listApiResponse.data.size());
            }
        });


    }

    @Override
    protected MainViewModel createViewModel() {
        return new ViewModelProvider(this,new MyMainViewModelFactory()).get(MainViewModel.class);
    }



}